/**
 * 
 */
package code.google.qinglan.xml;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author wxq
 * 
 */
public class Query {

	private Document document;
	private XpathSelector xpathsel;

	/**
	 * 
	 */
	public Query() {
	}

	public <T> List<T> queryForObjectList(String xpath, NodeMapper<T> rm) {
		List<T> resultList = new ArrayList<T>();
		NodeList ns = xpathsel.selectNodes(document, xpath);
		int len = ns.getLength();
		for (int i = 0; i < len; i++) {
			Node n = ns.item(i);
			resultList.add(rm.mapRow(n, i));
		}
		return resultList;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public XpathSelector getXpathsel() {
		return xpathsel;
	}

	public void setXpathsel(XpathSelector xpathsel) {
		this.xpathsel = xpathsel;
	}

}
